package com.yang.doctorrecruitment.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yang.doctorrecruitment.dao.CollectionDAO;
import com.yang.doctorrecruitment.dao.JobDAO;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class CollectionAction extends ActionSupport {
//    需要返回什么参数？保存结果就是成功失败，不需要用json就可以
    public JSON job;

    public String JobCollection(){
        ActionContext ac=ActionContext.getContext();
        HttpServletRequest request= (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
        String JobID=request.getParameter("JobID");
//        取出JobID
        Map session=ac.getSession();
        String UserID= (String) session.get("UserID");
//        取出UserID
//        执行操作
        Date date=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String PublishTime=df.format(new Date());

        boolean result= CollectionDAO.InsertCollection(UserID,JobID,PublishTime);
        JSONObject json=new JSONObject();
        if(result){
            json.put("result",1);
            job=json;
            return SUCCESS;
        }else{
//
            json.put("result",0);
            job=json;
            return ERROR;
        }
    }
    public String JobCollectionExist(){
        ActionContext ac=ActionContext.getContext();
        HttpServletRequest request= (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
        String JobID=request.getParameter("JobID");
//        取出JobID
        Map session=ac.getSession();
        String UserID= (String) session.get("UserID");
//        取出UserID
//        执行查找操作
        boolean result= CollectionDAO.SelectCollection(UserID,JobID);
        JSONObject json=new JSONObject();
         if(result){
             //已收藏
            json.put("exist",1);
            job=json;
            return SUCCESS;
        }else{
             //不存在
             json.put("exist",0);
            job=json;
             return ERROR;
        }
    }
    public String DeleteCollectionByJobID(){
        ActionContext ac=ActionContext.getContext();
        HttpServletRequest request= (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
        String JobID=request.getParameter("JobID");
//        取出JobID
        Map session=ac.getSession();
        String UserID= (String) session.get("UserID");
//        删除对应的UserID和jobID
        boolean res= JobDAO.deleteCollection(UserID,JobID);

        JSONObject json=new JSONObject();
        if(res){
            json.put("delete",1);
            job=json;
        }else{
            json.put("delete",0);
            job=json;
        }
        return SUCCESS;
    }

    public JSON getJob() {
        return job;
    }

    public void setJob(JSON job) {
        this.job = job;
    }
}
